package ${package.Controller};

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import ${package.Entity}.${entity};
import ${package.Service}.${entity}Service;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * ${table.comment} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@Slf4j
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} extends BaseController<${entity}>{
</#if>

    private String prefix = "${package.ModuleName}/${entityLowerName}";

    @Autowired
    ${entity}Service ${entityLowerName}Service;

    @GetMapping("/gotoSearch")
    public String gotoSearch(){
        return prefix + "/search";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageVO<${entity}> list(@RequestBody Map<String, Object> params){
        Page<${entity}> page = ${entityLowerName}Service.query${entity}Page(params);
        PageVO<${entity}> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @GetMapping("/gotoAdd")
	String gotoAdd(Model model){
        model.addAttribute("isEdit", false);
        model.addAttribute("${entityLowerName}", new ${entity}());
	    return prefix + "/add";
	}

	@GetMapping("/gotoEdit/{id}")
	String gotoEdit(@PathVariable("id") ${pkColType} ${pkColName}, Model model){
        ${entity} ${entityLowerName} = ${entityLowerName}Service.selectById(${pkColName});
		model.addAttribute("${entityLowerName}", ${entityLowerName});
        model.addAttribute("isEdit", true);
	    return prefix + "/add";
	}

	@Log("创建${table.comment}")
	@ResponseBody
	@PostMapping("/add")
	public ResMsg add(${entity} ${entityLowerName}){
        try{
            ${entityLowerName}Service.insert(${entityLowerName});
            return ResMsg.ok(new ${entity}());
        }catch(Exception e){
            log.error("创建${table.comment}失败", e);
            return ResMsg.error("创建${table.comment}失败");
        }

	}

    @Log("更新${table.comment}")
	@ResponseBody
	@PostMapping("/edit")
	public ResMsg edit(${entity} ${entityLowerName}){
        try{
            ${entityLowerName}Service.updateById(${entityLowerName});
            return ResMsg.ok();
        }catch(Exception e){
            log.error("更新${table.comment}失败", e);
            return ResMsg.error("更新${table.comment}失败");
        }

	}

	@Log("删除${table.comment}")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") ${pkColType} ${pkColName}){
        try{
            ${entityLowerName}Service.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}

	@Log("批量删除${table.comment}")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") ${pkColType}[] ${pkColName}s){
        ${entityLowerName}Service.deleteBatchIds(Arrays.asList(${pkColName}s));
		return ResMsg.ok();
	}

}

